<template>
    <div class="ComList">
        <draggable
            :list="state.componentsList"
            :group="state.comDragGroup"
            ghost-class="ghost"
            animation="300"
            @start="onStart"
            @end="onEnd"
            :move="onMove"
        >
            <template #item="{ element  }">
                <div
                    class="ComList-item"
                >
                    <div class="ComList-item-container">
                        <i
                            class="com-item__icon"
                            :style="{
                                backgroundImage: `url(${ element.icon })`,
                            }"
                        />
                        <span class="text">
                            {{ element.text }}
                        </span>
                    </div>
                </div>
            </template>
        </draggable>
       
    </div>
</template>

<script setup>
import {
  reactive,
  ref,
} from 'vue';

import draggable from 'vuedraggable';

import useCom from '@/store/com';
import { PreviewCom } from '@activity/common-components';
console.log('--------PreviewCom', PreviewCom);
const comStore = useCom();

// const componentsList = reactive(
//     Object.values(PreviewCom.previewComFormSchemas).map(com => ({
//         text: com.text,
//         icon: com.icon,
//         name: com.name,
//     }))
// )

const state = reactive({
    comDragGroup: {
        name: 'comDragGroup',
        pull: 'clone',
        put: false,
    },
    componentsList: Object.values(PreviewCom.previewComFormSchemas),
});
//拖拽开始的事件
const onStart = () => {
  console.log("开始拖拽");
  comStore.updateIsShowEmptyCom(true);
};
//拖拽结束的事件
const onEnd = (event) => {
  console.log("结束拖拽");
  const { newIndex } = event;
  const curDragComSchema = state.componentsList[newIndex];
  debugger
  comStore.updateIsDragEnd(true);
  comStore.updateIsShowEmptyCom(false);
  comStore.updateCurDragComSchema(curDragComSchema);
};
const onMove = (event) => {
    console.log('组件列表移动-------------------', event);
}


</script>

<style lang="less" scoped>
.ComList{
  position: absolute;
  top: 56px;
  left: 0;
  overflow-x: hidden;
  overflow-y: auto;
  bottom: 0;
  background: #fff;
  width: 176px;
  padding: 0 12px;
  box-sizing: content-box;
  &-item{
    position: relative;
    text-align: center;
    display: inline-block;
    width: 50%;
    height: 88px;
    font-size: 12px;
    margin-bottom: 8px;
    cursor: move;
    background: #fff;
    &-container{
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .com-item__icon{
            display: inline-block;
            margin-top: 8px;
            background-position: 0 0;
            height: 32px;
            width: 32px;
            background-size: cover;
        }
        .text {
            margin-top: 4px;
        }

        &:hover{
            background: #155bd4;
            border-radius: 2px;
            font-weight: 700;
            color: white;

            .com-item__icon{
            background-position: 0 32px;
            }
        }
    }

  }
}
.ghost{
    width: 100%;
    background-color: #ffffff;
    border: 2px dashed #5885df;
}
</style>